#define MAIN_CPP

#include "cod4map2.h"
#include "bsp_main.h"

using namespace std;

double FloatTime(){
	time_t t; 
	time(&t);
	return (double)t; 
}

void Init_Paths(int* argc, char** argv)
{
	int i, j, k;
	for(i = 0; i < *argc; i++){
		if(argv[i] == NULL){
			continue;
		}
		for(i = 0, j = 0, k = 0; i < *argc && j < *argc; i++, j++)
		{
			for( ; j < *argc && argv[j] == NULL; j++);
			argv[i] = argv[j];
			if(argv[i] != NULL)
			{
				cout << argv[i] << endl;
				k++;
			}
		}
			*argc = k;
			cout << *argc << endl;
	}
}


int main(int argc, char** argv){
	int i, r; 
	string argv_string; 
	double start, end; 

	BSP *d3dbsp = new BSP(); //Just read a .bsp straight in to this and close the ifstream?
	
	start = FloatTime();
	cout << "COD4Map2 (Silicone_Milk) -version " << COD4MAP_VERSION << endl;
	//cout << COD4MAP_MOTD << endl;
	
	for(i = 1; i < argc; i++)
	{ 
		argv_string = argv[i];

		/* Verbose Check */
		if(!argv_string.compare("-v")){
			//cout << "verbose is on!" << endl;
			argv[i] = NULL;
		}
		/* Number of Threads to Use */
		else if(!argv_string.compare("-threads")){
			argv[i] = NULL;
			i++;
			if(argv[i] != NULL){
				//cout << argv[i] << endl;
				argv[i] = NULL;
			}else{
				//cout << "You didn't specify the number of threads" << endl;
			}
		}
	}
    
	Init_Paths(&argc, argv); //get a valid working path and strip general options from the argv array
	if(argc < 2){
		cout << "Usage: " << argv[0] << "[general options] [options] mapfile" << endl;
		return 0;
	}
	argv_string = argv[1]; //We can *ASSUME* that this is a valid place in the argv array because the above if function didn't bail out.
	
	/* Decompile internal lightmaps found in the .d3dbsp file*/
	if(!argv_string.compare("-export")){
		r = d3dbsp->ExportLightmapsMain((argc - 2), argv + 1);
	}
	/* Compile external lightmaps in to the .d3dbsp file*/
	else if(!argv_string.compare("-import")){
		r = d3dbsp->ImportLightmapsMain(argc - 2, argv + 1);  //SAVE FOR LATER IMPLEMENTATION
	}
	else if(!argv_string.compare("-convert")){
		r = d3dbsp->ConvertBSPMain(argc - 2, argv + 1);
	}

	

	end = FloatTime();

	cout << showpoint << ((double)end - (double)start) << " seconds elapsed." << endl;


	d3dbsp = NULL;
    delete d3dbsp;
	return 0; 
}